package org.alino.core.shiro.token.realms;

import org.alino.core.Consts;
import org.alino.core.shiro.model.UserBase;
import org.alino.core.shiro.token.tokens.TokenByMobile;
import org.alino.core.utils.RequestUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.util.ByteSource;

/**
 * @Desc 手机账号密码授权登录
 * @Author by Brant
 * @Date 2017/1/13
 */
public class MobileRealm extends BaseRealm {
    public static final String BEAN_NAME = "mobileRealm";
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        TokenByMobile token = (TokenByMobile) authcToken;
        UserBase user = userService.findUserByMobile(token.getUsername());
        if(user == null || (user!=null && Consts.USER_STATUS_NOT_DELETED.equals(user.getStatus()))) {
            throw new UnknownAccountException("账号有误");//没找到帐号
        }else if(Consts.USER_STATUS_NOT_LOCKED.equals(user.getStatus())){
            throw new DisabledAccountException("账号已禁用,请联系客服");
        }
        user.setUserAgent(RequestUtils.getUserAgent(request));
        return new SimpleAuthenticationInfo(
                user, //用户名
                user.getPassword(), //密码
                ByteSource.Util.bytes(user.getMobile()+user.getSalt()), //味精
                getName());
    }
}
